﻿@{
    ViewBag.Title = "Quản lý các trường học";
    Layout = "~/Views/Shared/BE/_Admin_Layout.cshtml";
}
<article class="module width_3_quarter">
    <div id="SchoolTable"></div>
</article>
<script type="text/javascript"> $(document).ready(function() {
    //Prepare jtable plugin
    $('#SchoolTable').jtable({
        //...
        selecting: true, //Enable selecting
        multiselect: true, //Allow multiple selecting
        selectingCheckboxes: true, //Show checkboxes on first column
        openChildAsAccordion: true,
        paging: true,
        pageSize: 10,
        defaultSorting: 'SchoolName ASC',
        sorting: true,
        title: 'Danh sách các trường',
        actions: {
            listAction: '@Url.Action("ListSchool", "SchoolManagement")',
            deleteAction: '@Url.Action("DeleteSchool", "SchoolManagement")',
            updateAction: '@Url.Action("UpdateSchool", "SchoolManagement")',
            createAction: '@Url.Action("CreateSchool", "SchoolManagement")'
        },
        fields: {
            SchoolID: {
                key: true,
                create: false,
                edit: false,
                list: false
            },
            SchoolName: {
                title: 'Tên trường',
                width: '15%',
                inputClass: 'validate[required,maxSize[100]]'
            },
            TypeID: {
                title: 'Loại trường',
                options: '@Url.Action("GetSchoolTypeOptions","SchoolManagement")'
            },
            Address: {
                title: 'Địa chỉ',
                inputClass: 'validate[required,maxSize[250]]',
                type: 'textarea'
            },
            CityID: {
                title: 'Thành phố',
                options: '@Url.Action("GetCityOptions","SchoolManagement")',
                list: false,
                visibility: 'hidden'
            },
            DistrictID: {
                title: 'Quận',
                options: '@Url.Action("GetDistrictOptions","SchoolManagement")'
            },
            GoogleAPI: {
                title: 'GoogleAPI',
                type: 'textarea',
                visibility: 'hidden',
                list: false
            },
            Phone: {
                //create: false,
                title: 'Số điện thoại',
                visibility: 'hidden',
                list: false,
                inputClass: 'validate[maxSize[50],custom[phone]]'
            },
            Website: {
                //create: false,
                title: 'Địa chỉ website',
                visibility: 'hidden',
                list: false,
                inputClass: 'validate[maxSize[50]]'
            },
            Email: {
                //create: false,
                title: 'Địa chỉ email',
                visibility: 'hidden',
                list: false,
                inputClass: 'validate[maxSize[50],custom[email]]'
            },
            Principal: {
                title: 'Hiệu trưởng',
                visibility: 'hidden',
                list: false,
                inputClass: 'validate[maxSize[50]]'
            },
            Program: {
                title: 'Chương trình học',
                visibility: 'hidden',
                list: false,
                type: 'textarea'
            },
            StandardSS: {
                title: 'Sĩ số chuẩn',
                visibility: 'hidden',
                list: false,
                type: 'textarea'
            },
            Area: {
                title: 'Diện tích',
                visibility: 'hidden',
                list: false,
                inputClass: 'validate[required,custom[float],min[30]]'
            },
            AcceptAgeDescription: {
                title: 'Độ tuổi các bé',
                visibility: 'hidden',
                list: false,
                type: 'textarea'
            },
            Admission: {
                title: 'Tuyển sinh',
                visibility: 'hidden',
                list: false,
                type: 'textarea'
            },
            EstablishYear: {
                title: 'Năm thành lập',
                visibility: 'hidden',
                list: false,
                inputClass: 'validate[min[1],custom[IsInt]]'
            },
            NumberOfTeacher: {
                title: 'Tổng số giáo viên',
                visibility: 'hidden',
                list: false,
                inputClass: 'validate[required,min[1],custom[IsInt]]'
            },
            NumberOfChildren: {
                title: 'Tổng số học sinh',
                visibility: 'hidden',
                list: false,
                inputClass: 'validate[required,min[1],custom[IsInt]]'
            },
            TuitionFee: {
                title: 'Học phí',
                //visibility: 'hidden',
                //list: false,
                inputClass: 'validate[required,min[50],custom[IsInt]]'
            },
            EnrollmentTime: {
                title: 'Thời gian nhập học',
                visibility: 'hidden',
                list: false,
                type: 'textarea'
            },
            Description: {
                title: 'Ghi chú',
                visibility: 'hidden',
                list: false
                
            },
            ServiceList: {
                title: 'Các dịch vụ',
                list: false,
                visibility: 'hidden',
                type: 'radiobutton',
                options: '@Url.Action("GetServiceCheckList","SchoolManagement")'
            },
             Rating: {
                title: 'Rating',
                //visibility: 'hidden',
                //list: false
                
            },
        },
        formCreated: function(event, data) {
             data.form.validationEngine();
            //Phần mới
            //Change radio to checkbox
            var list = $('.jtable-radio-input #Edit-ServiceList');
            for(var tmpList = 0; tmpList< list.length; tmpList++) {
                list[tmpList].type = "checkbox";
            }
            var sid;
            try {
                sid = data.record.SchoolID;
            }catch(e) {
                sid = null;
            }
            if(sid != null) {
                var urlService = '@Url.Action("GetListService", "SchoolManagement")' + '?schoolID='  + sid;
                $.getJSON(urlService, { ajax: 'true' }, function(response) {
                    var row = response.Options;
                    for (var tmpRow = 0; tmpRow < row.length; tmpRow++) {
                        var com = row[tmpRow];
                        for (var i = 0; i < list.length; i++) {
                            if (list[i].value == com)
                                list[i].checked = true;
                        }
                    }
                    ;
                });
            }
            
            //
            var cboCity = data.form.find('select[name="CityID"]');
            var cboLine = data.form.find('select[name="DistrictID"]');
            cboLine.ready(function(e) {
                var cid = cboCity.val();
                // Load district Combo by cityID
                var url2 = '@Url.Action("GetDistrictOptions", "SchoolManagement")' + '?cityId=' + cid;
                $.getJSON(url2, { ajax: 'true' }, function(response) {
                    var result = response.Result;
                    var options = '';
                    cboLine.html(options);
                    if (result == 'OK') {
                        var rows = response.Options;
                        for (var k = 0; k < rows.length; k++) {
                            options += '<option value="' + rows[k].Value + '">' +
                                rows[k].DisplayText + '</option>';
                        }
                    }
                    cboLine.html(options);
                });
            });

            // City ComboBox Change
            cboCity.change(function(e) {
                var cid = $(this).val();
                // Refresh District Combo
                var url = '@Url.Action("GetDistrictOptions", "SchoolManagement")' + '?cityId=' + cid;
                $.getJSON(url, { ajax: 'true' }, function(response) {
                    var result = response.Result;
                    var options = '';
                    
                    cboLine.html(options);
                    if (result == 'OK') {
                        var rows = response.Options;
                        for (var i = 0; i < rows.length; i++) {
                            options += '<option value="' + rows[i].Value + '">' +
                                rows[i].DisplayText + '</option>';
                        }
                    }
                    cboLine.html(options);
                    cboLine.change();
                });

            });

            //Get Latitude and Longtitude by GoogleMap API
            $('#Edit-Address').change(function() {
                var schoolAddress = document.getElementsByName("Address")[0].value +
                    ' - ' + $("[name='DistrictID'] option:selected").text() + 
                    ' - ' + $("[name='CityID'] option:selected").text();
                console.log(schoolAddress);
                
                var geocoder = new google.maps.Geocoder();
                geocoder.geocode({ 'address': schoolAddress }, function (results, status) {
                    var latitude = results[0].geometry.location.lat();
                    var longitude = results[0].geometry.location.lng();
                    document.getElementsByName("GoogleAPI")[0].value = latitude + "," + longitude;
                    
                    //console.log('lat: ' + latitude + 'long: ' + longitude);
                });
            });
        },
        //Validate form when it is being submitted
            formSubmitting: function (event, data) {
                return data.form.validationEngine('validate');
            },
            //Dispose validation logic when form is closed
            formClosed: function (event, data) {
                data.form.validationEngine('hide');
                data.form.validationEngine('detach');
            }
    });
    //Re-load records when user click 'load records' button.
        $('#LoadRecordsButton').click(function (e) {
            e.preventDefault();
            $('#SchoolTable').jtable('load', {
                keywords: $('#name').val(),
            });
        });
    //Load person list from server
    $('#SchoolTable').jtable('load');
    
    //Delete selected students
        $('#DeleteAllButton').button().click(function () {
            var $selectedRows = $('#SchoolTable').jtable('selectedRows');
            $('#SchoolTable').jtable('deleteRows', $selectedRows);
        });
}); 
</script>